using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Portal.API;
using System.Web.UI;

namespace Portal
{
	/// <summary>
	///	Renders a Tab.
	/// </summary>
	public partial class PortalTab : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{

			PortalDefinition pd = PortalDefinition.Load();
			PortalDefinition.Tab tab = PortalDefinition.CurrentTab;

			if (tab.left.Count == 0)
			{
				tabMiddle.Style["margin-left"] = "0em";
			}
			if (tab.right.Count == 0)
			{
				tabMiddle.Style["margin-right"] = "0em";
			}
		}

		protected void OnAddTab(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load();
			PortalDefinition.Tab t = PortalDefinition.Tab.Create();

			pd.GetTab(Request["TabRef"]).tabs.Add(t);

			pd.Save();

			Response.Redirect(Helper.GetEditTabLink(t.reference));
		}

		protected void OnEditTab(object sender, EventArgs args)
		{
			Response.Redirect(Helper.GetEditTabLink());
		}

		protected void OnDeleteTab(object sender, EventArgs args)
		{
			PortalDefinition.Tab t = PortalDefinition.CurrentTab;
			PortalDefinition.DeleteTab(t.reference);

			if (t.parent == null)
			{
				Response.Redirect(Helper.GetTabLink(""));
			}
			else
			{
				Response.Redirect(Helper.GetTabLink(t.parent.reference));
			}
		}

		protected void OnAddLeftModule(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load();
			// Do NOT use GetCurrentTab! You will be unable to save
			PortalDefinition.Tab t = pd.GetTab(Request["TabRef"]);

			PortalDefinition.Module m = PortalDefinition.Module.Create();
			t.left.Add(m);

			pd.Save();

			Response.Redirect(Helper.GetEditModuleLink(m.reference));
		}

		protected void OnAddMiddleModule(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load();
			// Do NOT use GetCurrentTab! You will be unable to save
			PortalDefinition.Tab t = pd.GetTab(Request["TabRef"]);

			PortalDefinition.Module m = PortalDefinition.Module.Create();
			t.middle.Add(m);

			pd.Save();

			Response.Redirect(Helper.GetEditModuleLink(m.reference));
		}

		protected void OnAddRightModule(object sender, EventArgs args)
		{
			PortalDefinition pd = PortalDefinition.Load();
			// Do NOT use GetCurrentTab! You will be unable to save
			PortalDefinition.Tab t = pd.GetTab(Request["TabRef"]);

			PortalDefinition.Module m = PortalDefinition.Module.Create();
			t.right.Add(m);

			pd.Save();

			Response.Redirect(Helper.GetEditModuleLink(m.reference));
		}

		override protected void CreateChildControls()
		{
			PortalDefinition.Tab tab = PortalDefinition.CurrentTab;
			if (tab == null) return;

			if (UserManagement.HasViewRights(Page.User, tab.roles))
			{
				// Render
				RenderModules(tabLeft, tab, tab.left);
				RenderModules(tabMiddle, tab, tab.middle);
				RenderModules(tabRight, tab, tab.right);

				// Special case, if the middle part is not visible.
				//if (!tabMiddle.Visible && tabLeft.Visible && tabRight.Visible)
				//{
				//  tabLeft.Attributes["class"] = "tabLeftTwoCol";
				//  tabRight.Attributes["class"] = "tabRightTwoCol";
				//}
				if (tabMiddle.Visible && !tabLeft.Visible && !tabRight.Visible)
				{
					tabMiddle.Attributes["class"] = "tabMiddleFull";
				}
				if (tabMiddle.Visible && tabLeft.Visible && !tabRight.Visible)
				{
					tabMiddle.Attributes["class"] = "tabMiddleFull";
				}
			}
		}

		override protected void OnInit(EventArgs e)
		{
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			InitializeComponent();
			base.OnInit(e);
			EnsureChildControls();
		}

		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>

		private void InitializeComponent()
		{
		}

		private void RenderModules(HtmlGenericControl td, PortalDefinition.Tab tab, ArrayList modules)
		{
			if (modules.Count == 0)
			{
				td.Visible = false;
				return;
			}
			foreach (PortalDefinition.Module md in modules)
			{
				if (UserManagement.HasViewRights(Page.User, md.roles))
				{
					md.LoadModuleSettings();

					// Initialize the Module
					Control m = null;
					bool visible = false;
					try
					{
						// Is the Edit Mode Requested?
						if (Helper.IsEditModuleRequested(md))
						{
							// Load the Edit Mode of the module.
							m = Helper.GetEditControl(Page);
						}

						if (m == null)
						{
							// Load the View of the module.
							if (md.moduleSettings == null)
								m = LoadControl(AppConfig.GetModuleVirtualPath(md.type) + md.type + ".ascx");
							else
								m = LoadControl(AppConfig.GetModuleVirtualPath(md.type) + md.moduleSettings.ctrl);

							((Module)m).InitModule(tab.reference, md.reference, md.type,
								AppConfig.GetModuleDataVirtualPath(md.type), UserManagement.HasEditRights(Page.User, md.roles));

							visible = ((Module)m).IsVisible();
						}
						else
							visible = true;

						if (visible)
						{
							// Add Module Container
							HtmlGenericControl cont = new HtmlGenericControl("div");
							cont.Attributes.Add("class", "moduleContainer");
							td.Controls.Add(cont);

							// Add Module Header
							ModuleHeader mh = (ModuleHeader)LoadControl("ModuleHeader.ascx");
							mh.SetModuleConfig(md);
							cont.Controls.Add(mh);

							// Add Module Body Container
							HtmlGenericControl bodyCont = new HtmlGenericControl("div");
							bodyCont.Attributes.Add("class", "moduleContent");
							cont.Controls.Add(bodyCont);
							bodyCont.Controls.Add(m);
						}
					}
					catch (Exception e)
					{
						if (AppConfig.ShowModuleExceptions)
						{
							throw new Exception(e.Message, e);
						}
						// Add ModuleContainer
						HtmlGenericControl cont = new HtmlGenericControl("div");
						cont.Attributes.Add("class", "moduleContainer");
						cont.Controls.Add(m);
						td.Controls.Add(cont);

						// Add Module Header
						ModuleHeader mh = (ModuleHeader)LoadControl("ModuleHeader.ascx");
						mh.SetModuleConfig(md);
						cont.Controls.Add(mh);

						// Add Error Module
						ModuleFailed mf = (ModuleFailed)LoadControl("ModuleFailed.ascx");
						while (e != null)
						{
							mf.Message += e.GetType().Name + ": ";
							mf.Message += e.Message + "<br>";
							e = e.InnerException;
						}

						mf.Message = mf.Message.Remove(mf.Message.Length - 4, 4);

						HtmlGenericControl div = new HtmlGenericControl("div");
						div.Attributes.Add("class", "moduleContent");
						div.Controls.Add(mf);
						cont.Controls.Add(div);
					}
				}
			}
		}

	}
}
